//
//  ReadTableViewController.m
//  B
//
//  Created by lanou on 16/5/13.
//  Copyright © 2016年 裴启航. All rights reserved.
//

#import "ReadTableViewController.h"
#import "ReadTableViewCell.h"
#import "ReadTwoTableViewCell.h"
#import "ReadModel.h"
#import "ReadDetailViewController.h"

@interface ReadTableViewController ()
@property (nonatomic, strong) NSMutableArray *listArrs;
@property (nonatomic, assign) NSInteger count;
@end

@implementation ReadTableViewController

- (NSMutableArray *)listArrs
{
    if (!_listArrs) {
        _listArrs = [NSMutableArray array];
    }
    return _listArrs;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
    self.navigationItem.title = @"减肥养身";
    [self requestData];
    [self.tableView registerNib:[UINib nibWithNibName:@"ReadTableViewCell" bundle:nil] forCellReuseIdentifier:@"cell"];
    [self.tableView registerNib:[UINib nibWithNibName:@"ReadTwoTableViewCell" bundle:nil] forCellReuseIdentifier:@"twoCell"];
    
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(fresh)];
}

- (void)fresh
{
    self.count = 2;
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager GET:[NSString stringWithFormat:@"http://food.boohee.com/fb/v1/feeds?page=%ld&per=10", (long)self.count] parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSArray *array = responseObject[@"feeds"];
        for (NSDictionary *dic in array) {
            ReadModel *mode = [[ReadModel alloc] init];
            [mode setValuesForKeysWithDictionary:dic];
            [self.listArrs addObject:mode];
        }
        self.count += 1;
        [self.tableView.mj_footer endRefreshing];
        [self.tableView.mj_footer removeFromSuperview];
        [self.tableView reloadData];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];

}

- (void)requestData
{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager GET:@"http://food.boohee.com/fb/v1/feeds?page=1&per=10" parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSArray *array = responseObject[@"feeds"];
        for (NSDictionary *dic in array) {
            ReadModel *mode = [[ReadModel alloc] init];
            [mode setValuesForKeysWithDictionary:dic];
            [self.listArrs addObject:mode];
        }
        [self.tableView reloadData];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.listArrs.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ReadModel *model = self.listArrs[indexPath.row];
    if (!(model.background.length > 0)) {
        return 150;
    }
    return 200;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    ReadModel *model = self.listArrs[indexPath.row];
    if (!(model.background.length > 0)) {
        ReadTwoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"twoCell" forIndexPath:indexPath];
        cell.sourceLable.text = model.source;
        cell.contentLable.text = model.content;
        cell.tailLable.text = model.tail;

        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(10, 10, cell.frame.size.width - 20, cell.frame.size.height - 20)];
        view.backgroundColor = [UIColor whiteColor];
        view.alpha = 0.1;
        view.layer.borderWidth = 2.0f; //边框宽度
        view.layer.borderColor = [[UIColor blueColor] CGColor]; //边框颜色
        [cell addSubview:view];
        
        return cell;
    } else {
        ReadTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
        cell.sourceLable.text = model.source;
        cell.contentLable.text = model.content;
        cell.tailLable.text = model.tail;
        [cell.imageView1 sd_setImageWithURL:[NSURL URLWithString:model.background]];
        return cell;
    }
    
    // Configure the cell...
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    ReadModel *model = self.listArrs[indexPath.row];
    ReadDetailViewController *detail = [[ReadDetailViewController alloc] init];
    detail.string = model.link;
    [self.navigationController pushViewController:detail animated:YES];
}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
